package org.snail.chat.service.chatchain.quartz;

import lombok.extern.log4j.Log4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.snail.chat.service.impl.ChatServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import java.util.Date;

@Log4j
@Service
public class DingGroupQuartzJob implements Job {

    private ChatServiceImpl chatService;
    //TODO 自动配置 项目加载就可以启动
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //在job 中无法自动注入spring的bean还是用这种方法来注入
        WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
        ChatServiceImpl chatService2 = (ChatServiceImpl) wac.getBean("chatServiceImpl");
        this.chatService = chatService2;

        //里面自行写需要定时器执行的业务逻辑
        log.error("++++++++++++++++++++++此处执行触发器任务"+new Date());
        log.error("chatService:"+chatService);
        try {
            chatService.sychronizeUserDepatChat();
        } catch (Exception e) {
            log.error("定时器任务出错！详细错误请看错误信息及Log日志", e);
        }
    }
}
